package com.design.blog.controller;

import com.design.blog.model.R;
import com.design.blog.utils.QiniuUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

@RestController
@RequestMapping("upload")
public class UploadController {

    /*@PostMapping
    public R upload(@RequestParam("image") MultipartFile file, HttpSession session){
        Object downloadUrl = null;
        try {
            downloadUrl = ImgUtils.upload(file,session);
            if (downloadUrl != null) {
                return R.success(downloadUrl);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.fail(20001,"上传失败");
    }*/

    @PostMapping
    public R upload(@RequestParam("image")MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        boolean upload = QiniuUtils.upload(file, fileName);
        if (upload) {
            return R.success(QiniuUtils.url+fileName);
        }
        return R.fail(20000, "上传失败");
    }

}
